package de.baumann.browser.activitys.hash

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.support.design.widget.BottomSheetDialog
import android.support.v7.widget.LinearLayoutManager
import android.text.TextUtils
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.umeng.socialize.ShareAction
import com.umeng.socialize.UMShareAPI
import com.umeng.socialize.bean.SHARE_MEDIA
import com.umeng.socialize.media.UMImage
import com.umeng.socialize.media.UMWeb
import de.baumann.browser.OdinGlideModule
import de.baumann.browser.R
import de.baumann.browser.activitys.H5Activity
import de.baumann.browser.adapter.SendRecordAdapter
import de.baumann.browser.api.net.vo.PacketDetail
import de.baumann.browser.base.BaseOdinActivity
import de.baumann.browser.iview.IPacketDetailView
import de.baumann.browser.present.PacketDetailPresenter
import de.baumann.browser.utils.ViewUtil
import kotlinx.android.synthetic.main.activity_pack_detail.*
import kotlinx.android.synthetic.main.include_tool_bar.*
import java.util.*

/**
 *@author:junce
 *@date:2019/3/7 16:20
 *@discription: 红包信息
 */
class RedPacketDetailActivity : BaseOdinActivity<IPacketDetailView, PacketDetailPresenter>(), IPacketDetailView, View.OnClickListener {

    private lateinit var sendRecordAdapter: SendRecordAdapter
    private var type = 0

    companion object {

        fun startPacketDetailActivity(mContext: Context, id: Int) {
            val intent = Intent(mContext, RedPacketDetailActivity::class.java).apply {
                putExtra("id", id)
                putExtra("type", 1)
            }
            mContext.startActivity(intent)
        }


        fun startPacketDetailActivity(mContext: Context, id: Int, shareUrl: String?, amount: String?, adUrl: String) {
            val intent = Intent(mContext, RedPacketDetailActivity::class.java)
            intent.putExtra("id", id)
            intent.putExtra("shareUrl", shareUrl)
            intent.putExtra("amount", amount)
            intent.putExtra("adUrl", adUrl)
            mContext.startActivity(intent)
        }
    }

    override fun getTitleText(): String {
        return getString(R.string.Red_envelope_information)
    }

    override fun getLayout(): Int {
        return R.layout.activity_pack_detail
    }

    override fun initView() {
        initToolbar(commToolBar, commTitle)
        rvSendRecord.layoutManager = LinearLayoutManager(mContext)
        sendRecordAdapter = SendRecordAdapter(R.layout.recycler_item_send_record)
        rvSendRecord.adapter = sendRecordAdapter
        type = intent.getIntExtra("type", 0)
        if (type == 0) {
            rvSendRecord.visibility = View.GONE
//            ivRedShare.visibility = View.VISIBLE
//            ivRedShare.setOnClickListener(this)
        } else {
            textDetail1.visibility = View.GONE
            rvSendRecord.visibility = View.VISIBLE
        }
    }

    private fun shareWeb(platform: SHARE_MEDIA) {
        val umImage: UMImage? = if (shareThumb.isNotEmpty()) {
            UMImage(mContext, shareThumb)
        } else {
            UMImage(mContext, R.mipmap.ic_launcher)
        }

        val umweb = UMWeb(intent.getStringExtra("shareUrl"))
        umweb.title = "$shareName 请您助力"
        umweb.description = packetDesc
        umweb.setThumb(umImage)
        val shareAction = ShareAction(this)
        shareAction.withMedia(umweb)
        shareAction.platform = platform
        shareAction.share()
    }

    override fun createPresenter(): PacketDetailPresenter {
        return PacketDetailPresenter()
    }

    override fun createView(): IPacketDetailView {
        return this
    }


    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data)
    }

    override fun onClick(v: View?) {
        when (v!!.id) {
            R.id.shareToQQ -> {
                if (ViewUtil.isInstalledQQ(mContext)) {
                    shareWeb(SHARE_MEDIA.QQ)
                } else {
                    showShortToast(getString(R.string.not_installed_QQ))
                }
            }

            R.id.shareToWechart -> {
                if (ViewUtil.isInstalledWX(mContext)) {
                    shareWeb(SHARE_MEDIA.WEIXIN)
                } else {
                    showShortToast(getString(R.string.not_installed_WeChat))
                }
            }

            R.id.shareToWXCircle -> {
                if (ViewUtil.isInstalledWX(mContext)) {
                    shareWeb(SHARE_MEDIA.WEIXIN_CIRCLE)
                } else {
                    showShortToast(getString(R.string.not_installed_WeChat))
                }
            }

//            R.id.ivRedShare -> {
//                displaySharePanel()
//            }
        }
    }


    override fun initData() {
        presenter.packetDetails()
        val amount = intent.getStringExtra("amount")
        if (!TextUtils.isEmpty(amount)) {
            tvDetailAmount.text = amount
        }
    }

    override fun getPacketId(): Int {
        return intent.getIntExtra("id", 0)
    }

    override fun setHead(headUrl: String?) {
        val head = findViewById<ImageView>(R.id.ivDetailHead)
        if (this.isDestroyed) {
            return
        }
        OdinGlideModule.loadWithCropAndCircle(mContext, headUrl, head, R.drawable.icon_head)
    }

    @SuppressLint("SetTextI18n")
    private var shareName = ""

    override fun setName(name: String) {
        shareName = name
        findViewById<TextView>(R.id.tvDetailName).text = String.format(getString(R.string.red_envelope, name))
    }

    override fun setAmount(amount: String) {
        if (type == 0) {
            return
        }
       tvDetailAmount.text = amount
    }

    override fun setLocal(local: String) {
//        findViewById<TextView>(R.id.tvDetailLocal).text = local
    }

    override fun setAdUrl() {
        val adUrl = intent.getStringExtra("adUrl")
        if (adUrl == null || adUrl.isEmpty()) {
            return
        }

        tvDetailBanner.setOnClickListener {
            val intent = Intent(mContext, H5Activity::class.java)
            intent.putExtra("isUseTitle", false)

            if (adUrl.startsWith("http")) {
                intent.putExtra("url", adUrl)
            } else {
                intent.putExtra("url", "http://$adUrl")
            }
            startActivity(intent)
        }
    }

    private var shareThumb = ""
    override fun setBanners(type: Int, banners: ArrayList<PacketDetail.PacketEntity.ImgEntity>?) {

    }

    private var packetDesc = ""
    override fun setDesc(desc: String) {
        packetDesc = desc
       tvDetailDesc.text = desc
    }

    override fun setRecord(record: ArrayList<PacketDetail.ListEntity>?) {
        sendRecordAdapter.setNewData(record)
    }
}
